package com.ysg.widget.popup;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.PopupWindow;

public abstract class BasePopup {
    public Context context;
    public PopupWindow popupWindow;
    //弹出pop时，背景是否变暗
    private boolean isBackgroundDim = true;
    public BasePopup(Context context){
        this.context = context;
        init();
    }

    public abstract int getLayoutId();
    public abstract void initView(View view);

    /**
     * 如果子类的默认样式不一样，重写此方法
     */
    public void initAttributes() {
    }

    private void init() {
        if (popupWindow == null){
            popupWindow = new PopupWindow(context);
        }

        View view = LayoutInflater.from(context).inflate(getLayoutId(), null);
        initView(view);
        popupWindow.setContentView(view);
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true);//设置点击外部区域可以取消popupWindow
        popupWindow.setFocusable(true);//返回back键按下后,不会直接退出当前activity而是先退出当前的PopupWindow
        //重写属性
        initAttributes();

        //设置遮罩层
        if (isBackgroundDim){
            WindowManager.LayoutParams lp = ((Activity)context).getWindow().getAttributes();
            lp.alpha = 0.5f;
            ((Activity)context).getWindow().setAttributes(lp);
        }
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                //恢复遮罩层
                if (isBackgroundDim){
                    WindowManager.LayoutParams lp = ((Activity)context).getWindow().getAttributes();
                    lp.alpha = 1f;
                    ((Activity)context).getWindow().setAttributes(lp);
                }
            }
        });
    }

    public void show(View view){
        anchorView = view;
//        popupWindow.showAsDropDown(view, -408,-(view.getHeight()/2 + 50));
//        Log.e("getWidth", popupWindow.getContentView().getWidth()+"");
//        Log.e("getHeight", popupWindow.getContentView().getHeight()+"");
        if (0== popupWindow.getContentView().getWidth() && 0==popupWindow.getContentView().getHeight()){
            registerOnGlobalLayoutListener();
        }
        popupWindow.showAsDropDown(view, -popupWindow.getContentView().getWidth(), -(view.getHeight()/2 + popupWindow.getContentView().getHeight()/2));
    }

    /**
     * 注册GlobalLayoutListener 获取精准的宽高
     */
    private int mWidth, mHeight;
    private View anchorView;
    private void registerOnGlobalLayoutListener() {
        popupWindow.getContentView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                popupWindow.getContentView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
                mWidth = popupWindow.getContentView().getWidth();
                mHeight = popupWindow.getContentView().getHeight();
                Log.e("getWidth", popupWindow.getContentView().getWidth()+"");
                Log.e("getHeight", popupWindow.getContentView().getHeight()+"");
//                mWidth = 408;
//                mHeight = 100;
                popupWindow.update(-408, - 99, mWidth, mHeight);
            }
        });
    }

    public void dismiss(){
        popupWindow.dismiss();
    }

    public void setBackgroundDim(boolean backgroundDim) {
        isBackgroundDim = backgroundDim;
    }

    public void setWidth(int width){
        popupWindow.setWidth(width);
    }


}
